#include <iostream>
#include <boost/program_options.hpp>
#include <string>
#include <math.h>
#include <fstream>
#include <boost/filesystem/path.hpp>
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include "get_arg.h"


// link with -lboost_filesystem-gcc -lboost_program_options-gcc
// program_options_helper.cpp

#define DEBUG_LEVEL 6
#include <debug_func.h>


// my functions
#include <matrix_formats.h>
#include <print_error.h>
#include <program_options_helper.h>

using std::ofstream;
using std::ifstream;
using std::cout;
using std::string;
using std::cerr;
using namespace program_options_helper;
namespace po = boost::program_options_lg;


//_______________________________________________________________________________________

//		process_command_line_options

//				from command line
//				Created [9/13/2001]

//_______________________________________________________________________________________
bool process_command_line_options(int argc, char* argv[], t_program_args& args)
{
	// database login string
	std::string		dbname;

	//
	// checked mandatory arguments
	//
	po::options_description mandatory_args("Mandatory Arguments");
	mandatory_args.add_options()
	
	("dbname,d", 		po::value<string>(&dbname)
								->set_name("DATABASE"),
							"Existing PostgreSQL database name.")
	("table,t",			po::value<string>(&args.table)
								->set_name("TABLE"),
							"The name of the SQL table to place data.")
	("columns,c", 		po::value<string>(&args.columns)
								->set_name("COLUMNS"),
							"The names of the SQL columns containing the data:\n"
							"e.g. 'name TEXT, value REAL, total INTEGER, "
							"install_time TIMESTAMP'.\n"
							"An appropriate table will be created if necessary.");

	// database login string
	std::string		user;
	std::string		host;
	std::string		password;
	std::string		extra_login;

	string input_file_name;
	
	//____________________________________________________________________________________
	// panda login parameters

	po::options_description db_args("Optional Database Logon Arguments");
	db_args.add_options()
	("host,H",			po::value<string>(&host)
								->set_name("HOST"),
							"PostgreSQL database host computer address.")
	("user,u",			po::value<string>(&user)
								->set_name("USERNAME")
								->default_value("postgres"),
							"PostgreSQL database user name.")
	("password,p",		po::value<string>(&password)
								->set_name("PASSWORD"),
							"PostgreSQL database password.")
	("login,L",			po::value<string>(&extra_login)
								->set_name("OPTIONS"),
							"Other PostgreSQL login parameters in the form of "
							"keyword = value.\n"
							"E.g \"port=1234\"");
	//____________________________________________________________________________________

	args.optional_args.add_options()
	("input_file,i",	po::value<string>(&input_file_name)
								->set_name("FILE")
								->default_value("STDIN"),
							"File with tab delimited data PI linked data.\n"
							"Each entry is on a new line. "
							"The first four columns hold the 128-bit MD4 "
							"sequence hash as four 32-bit numbers. "
							"Defaults to STDIN")


	("remove_first,r",  po::bool_switch(&args.remove),
							"Removing existing data before update. ")
	("stats,s", 		po::bool_switch(&args.analyse),
							"Recalibrate database stats for faster queries.");

	args.add_std_options();
	po::options_description all_args("");
	all_args
						.add(mandatory_args)
						.add(db_args)
						.add(args.optional_args);

	// parse command line
	po::variables_map vm;
	po::store(po::command_line_parser(argc, argv).options(all_args).run(), vm);
	po::notify(vm);



	if (vm.count("version"))
	{
		print_version(cerr, *argv, "1.0");
		return false;
	}
	if (vm.count("help"))
	{
		string exe_description = "Synchronizes the Panda SQL Database after "
									"updates to a sequence database.\n\n";

		print_usage(cerr, string(*argv), "--dbname DATABASE\n"
										"--table TABLE\n"
										"--columns COLUMNS\n"
										"[OPTIONS]",
							all_args, exe_description, 80);
		return false;
	}

	check_required_options(vm, mandatory_args);

	//
	// initialize postgresql login
	//
	dbname	= "dbname=" + dbname;
	if (host.length())
		host = "host=" + host;
	if (password.length())
		password = "password=" + password;
	user = "user=" + user;
	args.db_login = dbname + ' ' + host + ' ' + password + ' ' + user + ' ' + extra_login;


	//
	// data input
	//
	if (input_file_name.length() && input_file_name != "STDIN")
	{
		args.input_file_strm.open(input_file_name.c_str());
		if (!args.input_file_strm.good())
			throw std::runtime_error("Could not open the input data file (" +
										input_file_name + ")");
		args.p_input_strm = &args.input_file_strm;
	}
	else
	{
		args.VERBOSE() << "\tData will be read from STDIN...\n";
		args.p_input_strm = &std::cin;
	}


	args.open_err_log();


	return true;
}















